<?
require_once("inc_security.php");
//check quyền them sua xoa
checkAddEdit("edit");
$returnurl = base64_decode(getValue("url","str","GET",base64_encode("listing.php")));


//Khai bao Bien
$errorMsg = "";
$iQuick = getValue("iQuick","str","POST","");
if ($iQuick == 'update'){
	$record_id = getValue("record_id", "arr", "POST", "");
	if($record_id != ""){
		for($i=0; $i<count($record_id); $i++){
			$errorMsg="";
			//Call Class generate_form();
			$myform = new generate_form();
			//Loại bỏ chuc nang thay the Tag Html
			$myform->removeHTML(0);
			//Insert to database
			$myform->add("adv_order", "adv_order_" . $record_id[$i], 0, 0, "", 0, "", 0, "");
			$myform->add("adv_link", "adv_link_" . $record_id[$i], 0, 0, "", 0, "", 0, "");
			$myform->add("adv_title", "adv_title_" . $record_id[$i], 0, 0, "", 0, "", 0, "");
			$myform->add("adv_position", "adv_position_" . $record_id[$i], 1, 0, "", 0, "", 0, "");
			$adv_startdate		= getValue("adv_startdate_" . $record_id[$i], "str", "POST", date("d/m/Y"));
			$adv_starttime		= getValue("adv_starttime_" . $record_id[$i], "str", "POST", date("H:i:s"));
			$adv_start			= convertDateTime($adv_startdate, $adv_starttime);	
			
			$adv_enddate		= getValue("adv_enddate_" . $record_id[$i], "str", "POST", date("d/m/Y", (time()+86400*300)));
			$adv_endtime		= getValue("adv_endtime_" . $record_id[$i], "str", "POST", date("H:i:s"));
			$adv_end				= convertDateTime($adv_enddate, $adv_endtime);	
					
			$myform->add("adv_start", "adv_start", 1, 1, 0, 0, "", 0, "");
			$myform->add("adv_end", "adv_end", 1, 1, 0, 0, "", 0, "");	
			$myform->add("admin_id", "admin_id", 1, 1, 0, 0, "", 0, "");		
			//Add table
			$myform->addTable($fs_table);
			
			$upload_pic = new upload("picture_" . $record_id[$i], $fs_filepath, $fs_extension, $fs_filesize);
			if ($upload_pic->file_name != ""){
				$picture = $upload_pic->file_name;
				//resize_image($fs_filepath,$upload_pic->file_name,100,100,75);
				$myform->add("adv_picture","picture",0,1,"",0,"",0,"");
			}
			if ($upload_pic->file_name != ""){
				//Delete file
				delete_file($fs_table,$id_field,$record_id[$i],"adv_picture",$fs_filepath);
				//Permision file
			}
			//Check Error!
			$errorMsg .= $upload_pic->show_warning_error();			
			
			$errorMsg .= $myform->checkdata();
			if($errorMsg == ""){
				$db_ex = new db_execute($myform->generate_update_SQL($id_field, $record_id[$i]));
				//echo $myform->generate_update_SQL($id_field, $record_id[$i]) . "<br>";
				unset($db_ex);
				//echo $errorMsg;
			}else{
				echo $errorMsg;
			}
			unset($myform);
		}
	}
	echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">';
	echo "Đang cập nhật dữ liệu !";
	redirect($returnurl);

}
?>